extends Node

@onready var player = $Player
@onready var mob_timer = $MobTimer
@onready var score_timer = $ScoreTimer
@onready var start_timer = $StartTimer
@onready var start_position = $StartPosition
@onready var mob_spawn_location = $MobPath/MobSpawnLocation
@onready var hud = $HUD
@onready var music = $Music
@onready var death_sound = $DeathSound

@export var mob_scene : PackedScene
var score

func _ready():
	#new_game()
	pass

func new_game():
	score = 0
	get_tree().call_group("mobs", "queue_free")
	
	player.start(start_position.position)
	start_timer.start()
	
	hud.update_score(0)
	hud.show_message("Get Ready")
	
	music.play()

func game_over():
	mob_timer.stop()
	score_timer.stop()
	
	hud.show_game_over()
	
	music.stop()
	death_sound.play()

func _on_score_timer_timeout():
	score += 1
	hud.update_score(score)

func _on_start_timer_timeout():
	mob_timer.start()
	score_timer.start()

func _on_mob_timer_timeout():
	var mob = mob_scene.instantiate()
	
	mob_spawn_location.progress_ratio = randf()
	
	var direction = mob_spawn_location.rotation + PI / 2;
	
	mob.position = mob_spawn_location.position;
	
	direction += randf_range(-PI / 4, PI /4)
	mob.rotation = direction
	
	var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
	mob.linear_velocity = velocity.rotated(direction);
	
	add_child(mob)
